package com.cnooc.aicv.lmapp.controller;

import com.cnooc.aicv.lmapp.model.LmAppMessage;
import com.cnooc.aicv.lmapp.model.LmResult;
import com.cnooc.aicv.lmapp.model.SafetyRisk;
import com.cnooc.aicv.lmapp.model.VideoAlarmInformation;
import com.cnooc.aicv.lmapp.service.JsonParserService;
import com.cnooc.aicv.lmapp.service.SafetyHatChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 安全生产报警二次验证
 * */
@Slf4j
@RestController
@RequestMapping("/api")
public class SafetyCheckController {

    public static final String NO_PRIV = "{\"message\",\"当前用户无权限访问数据，请来联系管理员！\"}";
    @Autowired
    private JsonParserService jsonParserService;
    @Autowired
    SafetyHatChecker safetyHatChecker;

    /**
     * 解析报警类型，图片，报警id 关键参数
     * 支持的类型公用一个接口：安全帽 ...
     * */
    @PostMapping("/alarmlmjudge")
    public String parseJsonAlarm(@RequestHeader("Authorization") String authorization, @RequestBody String json) throws Exception {
        log.debug("Authorization header: {}",  authorization);
        if(!testToken(authorization)){
            return NO_PRIV;
        }
        LmAppMessage lmAppMessage;
        try{
            VideoAlarmInformation videoAlarmInformation = jsonParserService.parseJsonVideoAlarm(json);
            //log.debug("alarm is:{}",videoAlarmInformation);
            log.debug("input str is: {}", videoAlarmInformation.toString());
            String base64 = videoAlarmInformation.getAlarmPictureBase64();
            String alarmType = videoAlarmInformation.getAlarmType();
//        try{
            LmResult lmResult = safetyHatChecker.sendSSLRequestPre(alarmType, base64);
            log.debug("lm message is :{}", lmResult.getMessage());
            lmAppMessage = jsonParserService.parseJsonLmLmResult(lmResult.getMessage());
            //      、、  ChatCompletion chatCompletion = jsonParserService.parseJsonChatCompletion(safetyRisk.getMessage());
        } catch (Exception e) {
            lmAppMessage = new LmAppMessage();
            lmAppMessage.setStatus("ERROR");//
            lmAppMessage.setMessage(e.getMessage());
            //throw new RuntimeException(e);
            log.error("Exception is :{}", e);
        }

        return  jsonParserService.toJsonLmAppMessage(lmAppMessage);
    }

    private boolean testToken(String authorization) {
        String TOKEN_ADMIN = "Bearer dhuiawbfuibqweibfiqwbicxsz156";
        return TOKEN_ADMIN.equals(authorization);    }

//    @PostMapping("/parse")
//    public LMDiagMsg parseJsonLmDiagMsg(@RequestHeader("Authorization") String authorization, @RequestBody String json) throws Exception {
//        LMDiagMsg lmDiagMsg;
//        log.info("Authorization header: {}",  authorization);
//        if(!testToken(authorization)){
//            lmDiagMsg = new LMDiagMsg();
//            lmDiagMsg.setModel(NO_PRIV);
//            return lmDiagMsg;
//        }
//        LMDiagMsg LMDiagMsg = jsonParserService.parseJsonLmDiagMsg(json);
//        log.info("aaa {}", LMDiagMsg.toString());
//        safetyHatChecker.sendSSLRequestDef();
//
//        return LMDiagMsg;
//    }


//
//    @PostMapping("/to-json")
//    public String toJson(@RequestBody LMDiagMsg request) throws Exception {
//        log.info("aaa {}", request);
//        return jsonParserService.toJson(request);
//    }
}